<?php
/**
 * Created by PhpStorm.
 * User: liziang
 * Date: 2019/1/4
 * Time: 13:29
 */
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;

use Illuminate\Support\Facades\Request;
use DB;
use \Illuminate\Support\Facades\Input;
use App\ModelStatistic\StatisticCommon;

class PlayerOrder extends Model{
    public static function paginate()
    {
        $accout = Request::get('account');
        $area = Request::get('cp_area')?:Request::get('area');
        $perPage = Request::get('per_page', 20);
        $page = Request::get('page', 1);

        $start = ($page-1)*$perPage;

        $tableAccount = 'platsdk_order';
        $rw = \DB::connection('mysql_bgac')->table($tableAccount)->where('cp_account','=',$accout);
        self::SourceCondition($rw);
        $total = $rw->get()->count();
        $rw = $rw->skip($start)->take($perPage);
        $data = $rw->get();
        $data = $data->toArray();
//        var_dump($data);exit();
        $alias_trans_list = \App\Models\Pay\Recharge::GetRechargeAlias();
        $recharge_json = \App\Models\Pay\Recharge::getAll();
        foreach ($data as &$obj) {
            $obj->prop_name = $alias_trans_list[$obj->cp_product_id ];
            
            $conf_one = $recharge_json[$obj->cp_product_id ]?:[];
            $obj->game_rmb = $conf_one['rmb'];
            $obj->game_dollar = $conf_one['dollar'];

            $obj->cp_rmb = round($obj->cp_rmb / 100, 2);
        }
        // $data = json_decode($data, true);
        \Log::debug(__METHOD__, [$rw->toSql()]);
        $list = static::hydrate($data);

        $paginator = new LengthAwarePaginator($list, $total, $perPage);

        $paginator->setPath(url()->current());
//var_dump($paginator);exit();
        return $paginator;
    }


    //模糊查询
    public static function SourceCondition(&$rw)
    {
        $cond = Request::all();
        !$cond['cp_area'] && $cond['cp_area'] = Request::get('area', 0);

        \Log::debug("[GET_PLAYER_ORDER]", [$cond]);
        if ($cond['OrderSerial']){
            $rw->where("OrderSerial", 'like', $cond["OrderSerial"].'%');
        }
//        if ($cond["OrderSerial"]){
//            $rw->where("OrderSerial", $cond['OrderSerial'] ?: $cond['OrderSerial']);
//        }
        $created_time = Request::get('created_time', []);
                
        $start_time = StatisticCommon::GetIntTime($created_time['start'] ? : $area_extend['open_time']);
        if ($start_time) {
            $rw->where('created_time', ">=",  $start_time);
        }

       
        if ($cond['cp_area'] && $cond['cp_area'] > 0) {
            $rw->where('cp_area','=', $cond['cp_area']);
        }

        $end_time = StatisticCommon::GetIntTime($created_time['end']);
        if ($end_time) {
            $rw->where('created_time', "<=", $end_time );
        }
        $rw->where('OrderSerial', '<>', '');
    }
}